package com.shiva.oms.biz;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.sf.shiva.oms.common.util.CollectionUtil;
import com.sf.shiva.oms.common.util.JsonUtil;
import com.shiva.oms.logview.domain.ConnectCfg;

/**
 * 
 * 将用户自定义的配置项保存到本地文件，用于下次启动时记忆
 * 
 * <pre>
 * HISTORY
 * ****************************************************************************
 *  ID    DATE            PERSON            REASON
 *  1     2018年4月16日         LA.01367604           Create
 * ****************************************************************************
 * </pre>
 * 
 * @author LA.01367604
 * @version 1.0
 */
public class CustomConnectCfgBiz extends AbstractCustomTemp<ConnectCfg> {

    private static final Logger logger = LoggerFactory.getLogger(CustomConnectCfgBiz.class);

    private static final String fileDir = "logv_conf";
    private static final String fileName = "custom.json";
    private File file = new File(CURRENT_DIR + FILE_SEP + fileDir + FILE_SEP + fileName);
    private List<ConnectCfg> cfgList;
    private static final CustomConnectCfgBiz instance = new CustomConnectCfgBiz();

    private CustomConnectCfgBiz() {}

    public static CustomConnectCfgBiz getInstance() {
        return instance;
    }

    @Override
    protected List<ConnectCfg> transStr2List(String str) {
        return JsonUtil.json2List(ConnectCfg.class, str);
    }

    public List<ConnectCfg> reloadList() {
        cfgList = super.init(fileDir, fileName, file);
        return cfgList;
    }

    public List<ConnectCfg> getCfgList() {
        if (cfgList == null) {
            cfgList = super.init(fileDir, fileName, file);
        }
        return cfgList;
    }

    public void addCfg(ConnectCfg cfg) {
        if (CollectionUtil.isEmpty(cfgList)) {
            cfgList = new ArrayList<>();
        }
        cfgList.add(cfg);
        saveAll();
    }

    public void removeCfg(String id) {
        super.remove2List(cfgList, id);
        saveAll();
    }

    public void updateCfg(String id, ConnectCfg newCfg) {
        super.update(cfgList, id, newCfg);
        saveAll();
    }

    public ConnectCfg getCfg(String id) {
        return super.getData(cfgList, id);
    }

    public void saveAll() {
        super.saveAll(cfgList, file);
    }

    @Override
    protected String transList2Str(List<ConnectCfg> list) {
        if (CollectionUtil.isEmpty(list)) {
            return "";
        } else {
            return JsonUtil.object2Json(list);
        }
    }

    public static void main(String[] args) {
        CustomConnectCfgBiz biz = CustomConnectCfgBiz.getInstance();
        System.out.println(biz.getCfgList().size());
//        ConnectCfg cfg = new ConnectCfg();
//        cfg.setId("2");
//        cfg.setCfgName("test-1");
//        cfg.setIp("12");
//        biz.addCfg(cfg);
//        // ConnectCfg newCfg = new ConnectCfg();
//        // newCfg.setId("2");
//        // newCfg.setCfgName("test-2");
//        // CustomCfgUtil.updateCfg("2", newCfg);
//        biz.reloadList();
//        System.out.println(biz.getCfgList().size());
//        biz.getCfgList().stream().forEach(data -> {
//            System.out.println("list- " + data.toString());
//        });
    }

}
